package api;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.traffic.api.jedis.JedisClient;

import redis.clients.jedis.Jedis;

public class SerializeUtil {
    public static void main(String [] args){
//        Jedis jedis = new Jedis("127.0.0.1",6379);
    	JedisClient jedis = JedisClient.getInstance();
//        String keys = "name";
//        // 删数据
//        //jedis.del(keys);
//        // 存数据
//        jedis.set(keys, "zy");
//        // 取数据
//        String value = jedis.get(keys);
        
        //存对象
        Person p=new Person();  //peson类记得实现序列化接口 Serializable
        p.setAge("20");
        p.setName("小晨");
        p.setId("1");
        byte[] b = "person".getBytes();
        jedis.set(b, serialize(p));
        
        byte[] byt=jedis.get(b);
        
        Object obj=unserizlize(byt);
        if(obj instanceof Person){
        }
    }
    
    //序列化 
    public static byte [] serialize(Object obj){
        ObjectOutputStream obi=null;
        ByteArrayOutputStream bai=null;
        try {
            bai=new ByteArrayOutputStream();
            obi=new ObjectOutputStream(bai);
            obi.writeObject(obj);
            byte[] byt=bai.toByteArray();
            return byt;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    //反序列化
    public static Object unserizlize(byte[] byt){
        ObjectInputStream oii=null;
        ByteArrayInputStream bis=null;
        bis=new ByteArrayInputStream(byt);
        try {
            oii=new ObjectInputStream(bis);
            Object obj=oii.readObject();
            return obj;
        } catch (Exception e) {
            
            e.printStackTrace();
        }
    
        
        return null;
    }
}
class Person implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4852555782520768210L;
	private String id;
	private String age;
	private String name;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}